﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Globalization;

namespace SrvMgr
{
   internal class ServiceNameConverter : TypeConverter
   {
       private string previousMachineName;
       private TypeConverter.StandardValuesCollection values;
       public ServiceNameConverter()
       {
       }
       public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
       {
           return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
       }
       public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
       {
           if (value is string)
           {
               return ((string)value).Trim();
           }
           return base.ConvertFrom(context, culture, value);
       }
       public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
       {
           ServiceController controller = (context == null) ? null : (context.Instance as ServiceController);
           string machineName = ".";
           if (controller != null)
           {
               machineName = controller.MachineName;
           }
           if ((this.values == null) || (machineName != this.previousMachineName))
           {
               try
               {
                   ServiceController[] services = ServiceController.GetServices(machineName);
                   string[] values = new string[services.Length];
                   for (int i = 0; i < services.Length; i++)
                   {
                       values[i] = services[i].ServiceName;
                       
                   }
                   this.values = new TypeConverter.StandardValuesCollection(values);
                   this.previousMachineName = machineName;
               }
               catch
               {
               }
           }
           return this.values;
       }
       public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
       {
           return false;
       }
       public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
       {
           return true;
       }

   }

    
    public partial class ucSrvMgr : UserControl
    {
        string name="";
        [TypeConverter(typeof(ServiceNameConverter)), SettingsBindable(true), ServiceProcessDescription("SPServiceName"), DefaultValue("")]
        public string ServiceName
        {
            get
            {               
                return this.name;
            }
            set
            {
                 
                    this.name = value;                    
                
            }
        }


        public string DisplayName
        {
            get
            {
                return groupBox1.Text;
            }
            set
            {
                groupBox1.Text = value;
            }

        }

        private ServiceController sc;
        public ucSrvMgr()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            sc.Start();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            sc.Stop();
        }

        private void btnContinue_Click(object sender, EventArgs e)
        {
            sc.Continue();
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            sc.Pause();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            btnContinue.Enabled = btnPause.Enabled = btnStart.Enabled = btnStop.Enabled = false;
            sc.Refresh();
            label1.Text = sc.Status.ToString();
            switch (sc.Status)
            {
                case ServiceControllerStatus.Stopped:
                    label1.BackColor = Color.Red;
                    btnStart.Enabled = true;
                    break;
                case ServiceControllerStatus.Running:
                    label1.BackColor = Color.Green;
                    btnPause.Enabled = btnStop.Enabled = true;
                    break;
                case ServiceControllerStatus.Paused:
                    label1.BackColor = Color.Yellow;
                    btnContinue.Enabled = btnStop.Enabled = btnStart.Enabled = true;
                    break;
                default:
                    label1.BackColor = Color.White;
                    break;
            }

        }

        private void ucSrvMgr_Load(object sender, EventArgs e)
        {
            try
            {
                sc = new ServiceController();
                if (this.ServiceName.Length > 0)
                {
                    sc.ServiceName = this.ServiceName;
                    groupBox1.Text = sc.DisplayName;
                    timer1.Enabled = true;
                }
                btnContinue.Enabled = btnPause.Enabled = btnStart.Enabled = btnStop.Enabled = false;
            }
            catch
            {
                timer1.Enabled = false;
            }
        }
    }
}
